package com.manage.system.aspect;

import com.manage.system.constant.Constants;
import com.manage.system.utils.TraceUtil;
import lombok.extern.log4j.Log4j2;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;

/**
 * @Author liuzhao
 * @Date 2023/2/27 21:32
 * @Content 定时任务添加traceId切面
 * @Version 1.0
 */
@Log4j2
@Aspect
@Component
public class JobLogAspect {

    @Pointcut("execution(* com.manage.system.job.* ..* (..))")
    public void doAspect() {

    }

    /**
     * 定时任务开始前添加traceId
     */
    @Before("doAspect()")
    public void beforeAspect() {
        String traceId = TraceUtil.getTraceId();
        MDC.put(Constants.TRACE_ID, traceId);
    }

    /**
     * 定时任务结束后移除traceId
     */
    @After("doAspect()")
    public void afterAspect() {
        MDC.remove(Constants.TRACE_ID);
    }
}
